package pl.edu.pjwstk.kor.sbql4j.project1;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.Struct;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project1Query_SbqlQuery3Db4o0 implements Db4oSBQLQuery {
    public Project1Query_SbqlQuery3Db4o0() {
    }

    /**
     * query='db.(Deal join  max(getCustomer().getRanks().getDiscount()))'
    '
     **/
    public java.util.Collection<pl.wcislo.sbql4j.java.model.runtime.Struct> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Deal> _ident_Deal = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Deal>();
        ClassMetadata _classMeta3 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Deal");
        long[] _ids3 = _classMeta3.getIDs(transLocal);

        for (long _id3 : _ids3) {
            LazyObjectReference _ref3 = transLocal.lazyReferenceFor((int) _id3);
            _ident_Deal.add((pl.edu.pjwstk.kor.model.Deal) _ref3.getObject());
        }

        java.util.Collection<pl.wcislo.sbql4j.java.model.runtime.Struct> _joinResult =
            new java.util.ArrayList<pl.wcislo.sbql4j.java.model.runtime.Struct>();
        int _joinIndex = 0;

        for (pl.edu.pjwstk.kor.model.Deal _joinEl : _ident_Deal) {
            if (_joinEl != null) {
                ocb.activate(_joinEl, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _mth_getCustomerResult = _joinEl.getCustomer();

            if (_mth_getCustomerResult != null) {
                ocb.activate(_mth_getCustomerResult, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _dotEl = _mth_getCustomerResult;

            if (_mth_getCustomerResult != null) {
                ocb.activate(_mth_getCustomerResult, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Rank> _mth_getRanksResult = _dotEl.getRanks();

            if (_mth_getRanksResult != null) {
                ocb.activate(_mth_getRanksResult, 2);
            }

            java.util.List<java.lang.Double> _dotResult1 = new java.util.ArrayList<java.lang.Double>();
            int _dotIndex1 = 0;

            for (pl.edu.pjwstk.kor.model.Rank _dotEl1 : _mth_getRanksResult) {
                if (_dotEl1 == null) {
                    continue;
                }

                if (_dotEl1 != null) {
                    ocb.activate(_dotEl1, 1);
                }

                java.lang.Double _mth_getDiscountResult = _dotEl1.getDiscount();

                if (_mth_getDiscountResult != null) {
                    ocb.activate(_mth_getDiscountResult, 1);
                }

                if (_mth_getDiscountResult != null) {
                    ocb.activate(_mth_getDiscountResult, 1);
                }

                _dotResult1.add(_mth_getDiscountResult);
                _dotIndex1++;
            }

            Number _max0 = null;

            for (Number _maxEl0 : _dotResult1) {
                _max0 = MathUtils.max(_max0, _maxEl0);
            }

            java.lang.Double _maxResult = (java.lang.Double) _max0;
            _joinResult.add(OperatorUtils.cartesianProductSS(_joinEl,
                    _maxResult, "", ""));
            _joinIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_joinResult, ocb);

        return _joinResult;
    }
}
